<?php

namespace App\Jobs;

use App\Models\ES\Media;
use App\Services\ElasticService;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use App\Models\ES\User AS esUser;
use App\Models\ES\Media AS esMedia;

class Elesticsearch implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $params;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($params)
    {
        $this->params = $params;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $type = (int)$this->params['type'];
        $action = $this->params['action'];
        $params_data = $this->params['data'];
        switch ($type) {
            case 1:
                $info = $this->addUser($params_data, $action);
                break;
            case 2:
                $info = $this->addMedia($params_data, $action);
                break;
        }
    }

    /*
     * 添加/编辑用户 搜索索引
     * */
    private function addUser($params_data, $action='add')
    {

        if(empty($params_data['uid']))
            return ['status'=>'error', 'message'=>'未知用户'];

        $esUser = new esUser();
        $es_user = $esUser->getByFid('uid',$params_data['uid']);
        // 添加
        if($action == 'add')
            if($es_user && $es_user['total']>0)
                return ['status'=>'error', 'message'=>'请勿重复添加数据'];

        $index  = $esUser->table();
        $index['id'] = $params_data['uid'];
        $index['body'] = $params_data;
        $info = $esUser->create_index($index);
        if(empty($info))
            return ['status'=>'error', 'message'=>'添加失败'];

        return ['status'=>'success', 'message'=>'添加成功'];
    }

    /*
     * 添加/编辑视频 搜索索引
     * */
    private function addMedia($params_data, $action='add')
    {
        $id = $params_data['mid'];
        $rows = \App\Models\Media::getRowsArray([['id', '=', $id]], ['id', 'uid', 'title', 'description', 'media', 'is_private']);
        $params_data['uid'] = $rows['uid'];
        $params_data['is_private'] = $rows['is_private'];

        if(empty($params_data['mid']))
            return ['status'=>'error', 'message'=>'未知视频'];

        $esMedia  = new esMedia();
        $es_media = $esMedia->getByFid('mid',$params_data['mid']);
        if($action == 'add'){
            if($es_media && $es_media['total']>0)
                return ['status'=>'error', 'message'=>'请勿重复添加数据'];
        }
        // 标识索引字段已删除
        else if($action == 'del'){
            if(empty($es_media['hits']))
                return ['status'=>'error', 'message'=>'索引数据不存在'];
            unset($params_data);
            $data = $es_media['hits'][0]['_source'];
            if(is_array($data))
                foreach ($data as $key=>$value)
                    $params_data['dele_'.$key] = $value;
        }

        $index  = $esMedia->table();
        $index['id'] = $id;
        $index['body'] = $params_data;
        $info = $esMedia->create_index($index);
        if(empty($info))
            return ['status'=>'error', 'message'=>'添加失败'];
        return ['status'=>'success', 'message'=>'添加成功'];
    }

}
